package com.maven.front.service.frontQuestions;

import com.maven.front.bean.*;
import com.maven.front.dto.EduQuestionsCommentDto;
import com.maven.front.dto.MyEduQuesstionDto;
import com.maven.front.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Service
public class QuestionsService {
    @Resource
    private EduUserMapper eduUserMapper;
    @Resource
    private EduQuestionsCommentMapper eduQuestionsCommentMapper;
    @Resource
    private EduQuestionsMapper eduQuestionsMapper;
    @Resource
    private EduQuestionsTagMapper eduQuestionsTagMapper;

    @Resource
    private EduQuestionsTagRelationMapper eduQuestionsTagRelationMapper;

//    public List<EduQuestionsDto> list(EduQuestionsDto eduQuestionsDto){
//            List<EduQuestions> list=eduQuestionsMapper.list(eduQuestionsDto);
//            List<EduQuestionsDto> list2=new ArrayList<EduQuestionsDto>();
//            list.stream().forEach(eduQuestions -> {
//                list2.add(change(eduQuestions));
//            });
//            return list2;
//    }
//
//    private EduQuestionsDto change(EduQuestions eduQuestions){
//       EduQuestionsDto edto=new EduQuestionsDto();
//        BeanUtils.copyProperties(eduQuestions,edto);
//        edto.setShowName(this.eduUserMapper.selectByPrimaryKey(eduQuestions.getCusId()).getShowName());
//        edto.setPicImg(this.eduUserMapper.selectByPrimaryKey(eduQuestions.getCusId()).getPicImg());
//        edto.setContent(this.eduQuestionsCommentMapper.selectByPrimaryKey(eduQuestions.getCusId()).getContent());
//        return edto;
//    }
//
//   public List<EduQuestionsTag> select(){
//        List<EduQuestionsTag> list=eduQuestionsTagMapper.select(null);
//        if(CollectionUtils.isEmpty(list)){
//            return null;
//        }else {
//            return list;
//        }
//   }
//
//   public EduQuestionsDto changeOne( int id){
//        EduQuestions e=eduQuestionsMapper.selectByPrimaryKey(id);
//        EduQuestionsDto ed=new EduQuestionsDto();
//        BeanUtils.copyProperties(e,ed);
//        ed.setShowName(this.eduUserMapper.selectByPrimaryKey(e.getCusId()).getShowName());
//        ed.setPicImg(this.eduUserMapper.selectByPrimaryKey(e.getCusId()).getPicImg());
//        ed.setContent(this.eduQuestionsCommentMapper.selectByPrimaryKey(e.getCusId()).getContent());
//    return ed;
//    }

    //所有问答-最佳回答/最新回复
    public List<MyEduQuesstionDto> selectAllDescByAddTimeOrView(MyEduQuesstionDto myEduQuesstionDto){
        List<EduQuestions> questions = eduQuestionsMapper.selectAllDescByAddTimeOrView(myEduQuesstionDto);
        List<EduQuestionsComment> comments  = eduQuestionsCommentMapper.selectAll();
        List<EduUser> users = eduUserMapper.selectByUserIds(questions);
       //排序
       comments = sort(comments);


        List<MyEduQuesstionDto> dtos = new ArrayList<>();
        for(EduQuestions q:questions){
            MyEduQuesstionDto dto = new MyEduQuesstionDto();
            BeanUtils.copyProperties(q,dto);
            //标签
            List<EduQuestionsTagRelation> list = eduQuestionsTagRelationMapper.selectByQuestionId(q.getId());
            List<EduQuestionsTag> tags = eduQuestionsTagMapper.selectByList(list);
            dto.setTags(tags);
            for(EduUser u:users){
                if(u.getUserId()==q.getCusId()){
                    dto.setEduUser(u);
                    break;
                }
            }
            for(EduQuestionsComment c:comments){
                if(c.getQuestionId()==q.getId()){
                    dto.setEduQuestionsComment(c);
                    break;
                }
            }
            dtos.add(dto);
        }
        return dtos;
    }

    //问题-所有回答
    public Map<String,Object> getById(int id){
        EduQuestions questions = eduQuestionsMapper.selectByPrimaryKey(id);
        //增加浏览次数
        questions.setBrowseCount(questions.getBrowseCount()+1);
        eduQuestionsMapper.updateByPrimaryKeySelective(questions);
        List<EduQuestionsTagRelation> list = eduQuestionsTagRelationMapper.selectByQuestionId(id);
        List<EduQuestionsTag> tags = eduQuestionsTagMapper.selectByList(list);
        List<EduQuestionsComment> comments = eduQuestionsCommentMapper.selectByQuestionId(id);
        EduUser user = eduUserMapper.selectByPrimaryKey(questions.getCusId());
        MyEduQuesstionDto dto = new MyEduQuesstionDto();
        BeanUtils.copyProperties(questions,dto);
        dto.setEduUser(user);
        dto.setTags(tags);
        List<EduQuestionsCommentDto> dtos = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        if(!ObjectUtils.isEmpty(comments)){
            List<EduUser> users = eduUserMapper.selectByUserIds(comments);
            for(EduQuestionsComment c:comments){
                EduQuestionsCommentDto commentDto = new EduQuestionsCommentDto();
                BeanUtils.copyProperties(c,commentDto);
                for(EduUser u:users){
                    if(u.getUserId()==c.getCusId()){
                        commentDto.setEduUser(u);
                        break;
                    }
                }
                dtos.add(commentDto);
            }
            map.put("comments",dtos);
        }
        map.put("eduQuestion",dto);
        return map;
    }


    //最佳回答-最新回复排序
    public List<EduQuestionsComment> sort(List<EduQuestionsComment> list){
        Collections.sort(list, new Comparator<EduQuestionsComment>() {
            @Override
            public int compare(EduQuestionsComment o1, EduQuestionsComment o2) {
                if(o1.getIsBest()!=o2.getIsBest()){
                    return o2.getIsBest().compareTo(o1.getIsBest());
                }
                return o2.getAddTime().compareTo(o2.getAddTime());
            }
        });
        return list;
    }

    public int saveComment(EduQuestionsComment comment){
        //添加子评论
        if(comment.getCommentId()!=0){
            int num = eduQuestionsCommentMapper.insertSelective(comment);
            EduQuestions questions = eduQuestionsMapper.selectByPrimaryKey(comment.getQuestionId());
            //提问回复+1
            questions.setReplyCount(questions.getReplyCount()+1);
            eduQuestionsMapper.updateByPrimaryKeySelective(questions);
            if(num>0){
               EduQuestionsComment parentComment = eduQuestionsCommentMapper.selectByPrimaryKey(comment.getCommentId());
               parentComment.setReplyCount(parentComment.getReplyCount()+1);
               return eduQuestionsCommentMapper.updateByPrimaryKeySelective(parentComment);
            }
        }
            //添加父评论
         return    eduQuestionsCommentMapper.insertSelective(comment);
    }


    public int addQuestionPraiseCount(EduQuestions questions){
        return eduQuestionsMapper.updateByPrimaryKeySelective(questions);
    }

    public int addCommentPraiseCount(EduQuestionsComment comment){
        return eduQuestionsCommentMapper.updateByPrimaryKeySelective(comment);
    }

    public int saveQuestion(MyEduQuesstionDto dto){
        EduQuestions questions = new EduQuestions();
        String[] str = dto.getQuestionsTag().split(",");
        List<Integer> list = new ArrayList<>();
        for(String s:str){
            if(!s.equals("")){
                list.add(Integer.parseInt(s));
            }
        }
        questions.setTitle(dto.getTitle());
        questions.setAddTime(new Date());
        questions.setContent(dto.getContent());
        questions.setType(dto.getType());
        questions.setCusId(dto.getCusId());
        int num = eduQuestionsMapper.insertSelective(questions);
        if(num>0){
            Map<String,Object> map = new HashMap<>();
            map.put("questionsId",questions.getId());
            map.put("list",list);
            return eduQuestionsTagRelationMapper.insertSelective(map);
        }else{
            return -1;
        }
    }

    //采纳最佳答案
    public int updateStatus(EduQuestionsComment comment){
        comment.setIsBest(1);
        eduQuestionsCommentMapper.updateByPrimaryKeySelective(comment);
        EduQuestions questions = eduQuestionsMapper.selectByPrimaryKey(comment.getQuestionId());
        questions.setStatus(1);
        return eduQuestionsMapper.updateByPrimaryKeySelective(questions);
    }
}
